package com.ll.blog.common;

import com.alibaba.fastjson.JSONObject;
import com.ll.blog.contant.ExceptionEnum;
import lombok.*;

import java.io.Serializable;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> implements Serializable {
    private Integer code;
    private String msg;
    private  T data;
    public static final Integer SUCCESS_CODE = 10000;
    public static final String SUCCESSW_MSG = "请求成功";

    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",data);
        jsonObject.put("msg",msg);
        jsonObject.put("code",code);
        return jsonObject.toString();
    }

    public ApiResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T>  ApiResponse<T> success(T data){
        return new ApiResponse(SUCCESS_CODE,SUCCESSW_MSG,data);
    }
    public static <T>  ApiResponse<T> success(){
        return new ApiResponse(SUCCESS_CODE,SUCCESSW_MSG);
    }
    public static <T>  ApiResponse<T> success(String msg){
        return new ApiResponse(SUCCESS_CODE,msg);
    }
    public static <T>  ApiResponse<T> success(String msg,T data){
        return new ApiResponse(SUCCESS_CODE,msg,data);
    }
    public static ApiResponse error(Integer code, String msg){
        return new ApiResponse(code,msg);
    }
    public static ApiResponse error(ExceptionEnum exceptionEnum){
        return new ApiResponse(exceptionEnum.getCode(),exceptionEnum.getMsg());
    }

}
